﻿using Publicuse.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    public class DBHelperUtil<T>
    {
        private static Dictionary<int, T> pInst;
        private static readonly object locker = new object();

        private static DBHelperUtil<T> instance = null;

        static DBHelperUtil()
        {
            instance = new DBHelperUtil<T>();
            pInst = new Dictionary<int, T>();
        }

        public static DBHelperUtil<T> GetDbHelper
        {
            get { return instance; }
        }

        public void AddInstance(string constr, int DBI)
        {
            if (pInst == null)
                return;

            lock (locker)
            {
                if (!pInst.ContainsKey(DBI))
                {
                    Type type = typeof(T);
                    T t = (T)Activator.CreateInstance(type);
                    var method = type.GetMethod("Init");
                    method.Invoke(t, new object[] { constr });
                    pInst.Add(DBI, t);

                }
            }
        }

        public static T GetInstance(int iType)
        {
            if (!pInst.ContainsKey(iType))
                return default(T);

            return pInst[iType];
        }

        public static void RemoveInstance(int dbi)
        {
            lock (locker)
            {
                if (pInst.ContainsKey(dbi))
                {
                    pInst.Remove(dbi);
                }
            }
        }
        public static void ClearInstance()
        {
            lock (locker)
            {
                pInst.Clear();
            }
        }
    }

    public enum SqlParamsType : int
    {
        Default = 1,

        Params = 2
    }

    public class CURDModel
    {
        public CUDType cUDType { get; set; }

        public SqlParamsType sqlParamsType { get; set; }
    }
}
